package com.hyc.myvideo.ui.activity.my;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.my.order.OrderAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.ModelOrder;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的购买
 * @author gyb
 * @version 1.0
 * @created 2017/07/18
 */
public class PurchasingActivity extends BaseFActivity implements AdapterView.OnItemClickListener{
    @BindView(R.id.common_head_list_id)
    PullToRefreshListView toRefreshListView;
    /**页数*/
    private int page = 1;
    /**是否刷新*/
    private boolean isRefresh = true;
    private OrderAdapter adapter = null;
    private List<ModelOrder> ListData = null;
    @Override
    public int getContentViewId() {
        return R.layout.activity_common_listview_head;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true,getIntent().getStringExtra("title"));
        toRefreshListView.setOnItemClickListener(this);
        ListData = new ArrayList<>();
        adapter = new OrderAdapter(this,ListData);
        toRefreshListView.setAdapter(adapter);
        toRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){
                requestRegister(true);
            }

            @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestRegister(false);
            }
        });
    }




    @Override
    protected void initData() {
        requestRegister(true);
    }

    /***
     * 请求
     * @param isRefresh 是否刷新
     */
    private void requestRegister(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            page = 1;
        }else{
            page++;
        }
        api.requestOrder(page);
    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<List<ModelOrder>> result = JSONObject.parseObject(r, new TypeReference<BaseResult<List<ModelOrder>>>(){});
        if(result.isCode()){
            if(isRefresh){
                ListData.clear();
            }
            ListData.addAll(result.getResult());
            adapter.updateListView(ListData);
        }
        clearedListView();
    }


    @Override
    protected void oError() {
        clearedListView();
    }
    /***
     * 关闭刷新
     */
    private void clearedListView(){
        if(toRefreshListView.isRefreshing()){
            toRefreshListView.onRefreshComplete();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int size = toRefreshListView.getRefreshableView().getHeaderViewsCount();
        if (ListData != null && !ListData.isEmpty()) {
            ModelOrder order = ListData.get(position - size);
            if (order != null) {
                String o_id = order.getDirect_id();
                if (!StringUtils.isEmpty(o_id)) {
//                    startActivity(new Intent(this, CourseDetailsActivity.class).putExtra("b_id",o_id).putExtra("type",0));
                }
            }
        }
    }
}
